package com.yitiao.controller;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/config")
@RequiredArgsConstructor
public class NacosConfigController {
    private final NacosConfigManager configManager;

    private final NacosConfigProperties nacosConfigProperties;

    @GetMapping
    public void getConfig() throws NacosException {
        System.out.println(configManager.getConfigService()
                .getConfig("cloud-alibaba-nacos.yml", nacosConfigProperties.getGroup(), 5000));
    }


    @PostMapping
    public void setConfig() throws NacosException {
        configManager.getConfigService()
                // 全量替换
                .publishConfig("cloud-alibaba-nacos.yml",
                        nacosConfigProperties.getGroup(),
                        "thread-pool:\n" +
                                "  core-size: 10\n" +
                                "  max-size: 20",
                        ConfigType.YAML.getType());
    }

}
